package com.smartguard.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.smartguard.bean.Device;
import com.smartguard.bean.User;
import com.smartguard.bean.Video;
import com.smartguard.service.DeviceService;
import com.smartguard.service.VideoService;


public class videoStream extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public videoStream() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}


	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		
		

	//	String filePath = request.getRealPath("/") + "videos/";

		String filePath = "d:/videos/";
		HttpSession session = request.getSession(false);
		
		User ref = (User) session.getAttribute("user");
//		Device dev = (Device) session.getAttribute("device");
		String id = request.getParameter("guid");
		System.out.println(id);
		
		DeviceService ds = new DeviceService();
		VideoService vs = new VideoService();
		Device td = ds.findDeviceByDeviceHash(id);
		
		
		
		
		filePath = filePath+ref.getUsername()+"/";
		File f = new File(filePath);
		if (!f.exists()) {
			f.mkdirs();
		}
		
		SimpleDateFormat format = new SimpleDateFormat("yyMMdd-HHmm");
		Date date = new Date(System.currentTimeMillis());
//		System.out.println(format.format(date));
		
		InputStream in_nocode = request.getInputStream();
		String src = "/videos/" + ref.getUsername()+"/"
				+ format.format(date) + ".webm";
		OutputStream outputStream = new FileOutputStream(filePath
				+ format.format(date) + ".webm");
		  int c;
          while((c=in_nocode.read())!=-1)
          {
          	outputStream.write(c);
          }
          outputStream.flush();
          
          SimpleDateFormat bformat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
          //写入数据库
          Video video = new Video();
          video.setDate(bformat.format(date));
          video.setUsername(ref.getUsername());
          video.setSrc(src);
          video.setDeviceHash(td.getDeviceHash());
          if(td.getDeviceName() != null)
        	  video.setDeviceName(td.getDeviceName());
          
          vs.insertVideo(video);
          
		
		outputStream.close();
		System.out.println("record finish");
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}
	

}
